package com.wt.admin.service.image.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.code.image.ImageItem3200;
import com.wt.admin.domain.dto.image.ImageVideoDTO;
import com.wt.admin.domain.entity.image.ImageClassificationEntity;
import com.wt.admin.domain.entity.image.ImageVideoEntity;
import com.wt.admin.domain.vo.image.ImageVideoVO;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.mapper.image.ImageVideoMapper;
import com.wt.admin.service.file.UploadFileService;
import com.wt.admin.service.image.ImageClassificationService;
import com.wt.admin.service.image.ImageVideoService;
import com.wt.admin.util.AssertUtil;
import com.wt.admin.util.FileUtils;
import com.wt.admin.util.PageUtil;
import com.wt.admin.util.VideoUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.beans.Transient;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Service("video")
public class ImageVideoServiceImpl extends ServiceImpl<ImageVideoMapper, ImageVideoEntity> implements ImageVideoService, UploadFileService {

    @Resource
    ImageVideoMapper imageVideoMapper;
    @Resource
    private ImageClassificationService imageClassificationService;

    @Override
    public PageUtil.PageVO<ImageVideoVO> videoList(PageUtil.PageDTO<ImageVideoDTO> data) {
        return PageUtil.findPage(data, (page) -> imageVideoMapper.videoList(page, data.getData()));
    }

    @Override
    public ImageVideoVO delVideo(List<String> data) {
        AssertUtil.List.isEmpty(data, ImageItem3200.CODE_3204);
        removeByIds(data);
        return null;
    }

    @Override
    public boolean exec(String path, MultipartFile file, Map<String, String[]> map, UserVO userVO) {
        try {
            String classificationId = map.get("classificationId")[0];
            String languageClassificationId = map.get("languageClassificationId")[0];
            ImageClassificationEntity imageClassificationEntity = imageClassificationService.byId(Integer.valueOf(classificationId));
            ImageVideoEntity imageVideo = new ImageVideoEntity();
            imageVideo.setUrl(newUrl(path,imageClassificationEntity));
            imageVideo.setClassificationId(Integer.valueOf(classificationId));
            imageVideo.setLanguageClassificationId(Integer.valueOf(languageClassificationId));
            save(imageVideo);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transient
    @Override
    public boolean batchExec(Map<String, MultipartFile> pathMap, Map<String, String[]> map, UserVO userVO) {
        String classificationId = map.get("classificationId")[0];
        String languageClassificationId = map.get("languageClassificationId")[0];
        ImageClassificationEntity imageClassificationEntity = imageClassificationService.byId(Integer.valueOf(classificationId));
        List<ImageVideoEntity> list = pathMap.keySet().stream().map(i -> {
            try {
                ImageVideoEntity imageVideo = new ImageVideoEntity();
                imageVideo.setUrl(newUrl(i,imageClassificationEntity));
                imageVideo.setClassificationId(Integer.valueOf(classificationId));
                imageVideo.setLanguageClassificationId(Integer.valueOf(languageClassificationId));
                return imageVideo;
            }catch (Exception e){
                return null;
            }
        }).filter(ObjUtil::isNotEmpty).toList();
        saveBatch(list);
        return true;
    }

    private String newUrl(String path,ImageClassificationEntity obj) throws IOException {
        String url = FileUtils.getImageAbsURL(path);
        String newUrl = FileUtils.newImageAbsURL(path);
        VideoUtil.resizeVideoWH(url, FileUtils.getImageAbsURL(newUrl), obj.getImageWidth().intValue(), obj.getImageHeight().intValue());
        FileUtil.del(url);
        return newUrl;
    }

}
